package iterator;

import java.util.*;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/7/3 15:23
 */
public class MapIterator {
    public static void main(String[] args) {
        Map<String,String> maps = new HashMap<String, String>();
        maps.put("person1","168");
        maps.put("person2","160");
        maps.put("person3","172");
        maps.put("person4","170");
        //1.增强for循环遍历(使用较多)
        for(String s:maps.keySet()){
            System.out.println(s+":"+maps.get(s));
        }
        //2.迭代器遍历(根据entrySet来遍历)
        Iterator<Map.Entry<String, String>> entries = maps.entrySet().iterator();
        while (entries.hasNext()){
            Map.Entry<String,String> map = entries.next();
            System.out.println(map.getKey()+":"+map.getValue());
        }
        //3.迭代器遍历(根据keySet来遍历)
        Set<String> names = maps.keySet();
        Iterator<String> name = names.iterator();
        while(name.hasNext()) {
            String key = name.next();
            System.out.println(key +":"+maps.get(key));
        }
        System.out.println("----------------------");
        //4.增强for结合Map.Entry(容量大时使用)
        for(Map.Entry<String, String> s:maps.entrySet()){
            System.out.println(s.getKey()+":"+s.getValue());
        }
    }
}
